package me.gm.stcont.controller;

import javax.validation.Valid;

import me.gm.stcont.Utils;
import me.gm.stcont.model.Individual;
import me.gm.stcont.validator.ClientValidator;
import me.gm.stcont.validator.PhoneValidator;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping(value = "/client")
public class ClientController extends BaseController {

	public ClientController() {
	}

	@InitBinder("individual")
	public void initClientBinder(WebDataBinder webDataBinder) {
		webDataBinder.setValidator(new ClientValidator());
	}

	@InitBinder("phone")
	public void initPhoneBinder(WebDataBinder webDataBinder) {
		webDataBinder.setValidator(new PhoneValidator());
	}

	@RequestMapping(value = "/main")
	public String main() {
		getLogger().debug("started main");
		getLogger().debug("finished main");

		return "client/main";
	}

	@RequestMapping(value = "/list")
	public String list(@RequestParam(required = true) Integer page,
			@RequestParam(required = true) String like, Model model) {
		getLogger().debug("started list");
		getLogger().debug(page);

		String convertedLike;

		convertedLike = Utils.convertToUtf8(like);

		getLogger().debug(convertedLike);

		model.addAttribute("like", convertedLike);

		Page<Individual> individuals;

		Sort sort;

		sort = new Sort("nickName", "name", "id");

		if (StringUtils.isBlank(convertedLike)) {
			individuals = getIndividualRepository().findByType(
					Individual.Type.CLIENT, getPageable(page, 20, sort));
		} else {
			String likeNickName;

			likeNickName = "%" + convertedLike + "%";

			individuals = getIndividualRepository().findByTypeAndNickNameLike(
					Individual.Type.CLIENT, likeNickName,
					getPageable(page, 20, sort));
		}

		model.addAttribute("individuals", individuals);

		getLogger().debug("finished list");

		return "client/list";
	}

	@RequestMapping(value = "/edit/{id}")
	public String edit(@PathVariable("id") Integer id,
			@RequestParam(required = true) Integer page,
			@RequestParam(required = true) String like, Model model) {
		getLogger().debug("started edit");
		getLogger().debug(id);
		getLogger().debug(page);

		String convertedLike;

		convertedLike = Utils.convertToUtf8(like);

		getLogger().debug(convertedLike);
		getLogger().debug(model);

		model.addAttribute("page", page);
		model.addAttribute("like", convertedLike);

		Individual individual;

		if (id.intValue() == 0) {
			individual = new Individual();
			individual.setId(0);
			individual.setEnabled(true);
			individual.setType(Individual.Type.CLIENT);
		} else {
			individual = getIndividualRepository().findOne(id);
		}

		initEdit(model);

		model.addAttribute("individual", individual);

		getLogger().debug("finished edit");

		return "client/edit";
	}

	private void initEdit(Model model) {
	}

	@RequestMapping(value = "/delete/{id}")
	public String delete(@PathVariable("id") Integer id,
			@RequestParam(required = true) String like) {
		getLogger().debug("started delete");
		getLogger().debug(id);

		String convertedLike;

		convertedLike = Utils.convertToUtf8(like);

		getLogger().debug(convertedLike);

		getIndividualRepository().delete(id);

		getLogger().debug("finished delete");

		return "redirect:/client/list?page=0&like=" + like + "&_="
				+ System.currentTimeMillis();
	}

	@RequestMapping(method = RequestMethod.POST)
	public String save(
			@Valid @ModelAttribute("individual") Individual individual,
			BindingResult bindingResult,
			@RequestParam(required = true) Integer page,
			@RequestParam(required = true) String like, Model model) {
		getLogger().debug("started save");
		getLogger().debug(individual);
		getLogger().debug(bindingResult);
		getLogger().debug(page);

		String convertedLike;

		convertedLike = Utils.convertToUtf8(like);

		getLogger().debug(convertedLike);
		getLogger().debug(model);

		if (bindingResult.hasErrors()) {
			model.addAttribute("page", page);
			model.addAttribute("like", convertedLike);

			initEdit(model);

			return "client/edit";
		}

		getIndividualRepository().save(individual);

		getLogger().debug("finished save");

		return "redirect:/client/list?page=" + page + "&like=" + like + "&_="
				+ System.currentTimeMillis();
	}

}
